package com.dobbinsoft.iotechn.ability.openapi;

import com.dobbinsoft.iotechn.ability.annotation.DeviceOpenApi;
import com.dobbinsoft.iotechn.ability.annotation.DeviceOpenApiMethod;
import com.dobbinsoft.iotechn.ability.annotation.DeviceOpenApiRetDescription;
import com.dobbinsoft.iotechn.ability.model.AbilityContext;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 * User: rize
 * Date: 2020/7/7
 * Time: 17:06
 */
@DeviceOpenApi(group = "time", description = "提供系统时间服务")
public class Time {

    /**
     * 返回从 格林时间 1970-01-01 00:00:00 到 现在的秒数
     * @return
     */
    @DeviceOpenApiMethod(
            description = "获取时间戳(秒)",
            example = "{\"timestamp\":1594099420}",
            retDescription = {
                    @DeviceOpenApiRetDescription(name = "timestamp", example = "1594099420", description = "从1970年1月1日到现在所度过的秒数（格林威治时间，注意时区）")
            })
    public Object getGmt(AbilityContext abilityContext) {
        Map<String, Object> map = new HashMap<>();
        map.put("timestamp", System.currentTimeMillis() / 1000);
        return map;
    }

    @DeviceOpenApiMethod(
            description = "获取时间日期制",
            example = "{\"month\":6,\"hour\":12,\"year\":2020,\"day\":7,\"minute\":37,\"second\":40}",
            retDescription = {
                    @DeviceOpenApiRetDescription(name = "year", example = "2020", description = "当前年"),
                    @DeviceOpenApiRetDescription(name = "month", example = "6", description = "当前月"),
                    @DeviceOpenApiRetDescription(name = "day", example = "7", description = "当前日"),
                    @DeviceOpenApiRetDescription(name = "hour", example = "12", description = "当前时"),
                    @DeviceOpenApiRetDescription(name = "minute", example = "37", description = "当前分"),
                    @DeviceOpenApiRetDescription(name = "second", example = "40", description = "当前秒")
            })
    public Object getGmtDate(AbilityContext abilityContext) {
        Map<String, Object> map = new HashMap<>();
        Calendar instance = Calendar.getInstance();
        map.put("year", instance.get(Calendar.YEAR));
        map.put("month", instance.get(Calendar.MONTH) + 1);
        map.put("day", instance.get(Calendar.DAY_OF_MONTH));
        map.put("hour", instance.get(Calendar.HOUR_OF_DAY));
        map.put("minute", instance.get(Calendar.MINUTE));
        map.put("second", instance.get(Calendar.SECOND));
        return map;
    }

}
